package com.hzw.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.hzw.subject.common.enums.IsDeletedFlagEnum;
import com.hzw.subject.domain.convert.SubjectCategoryConverter;
import com.hzw.subject.domain.convert.SubjectLabelConverter;
import com.hzw.subject.domain.entity.SubjectCategoryBO;
import com.hzw.subject.domain.entity.SubjectLabelBO;
import com.hzw.subject.domain.service.SubjectLabelDomainService;
import com.hzw.subject.infra.basic.entity.SubjectCategory;
import com.hzw.subject.infra.basic.entity.SubjectLabel;
import com.hzw.subject.infra.basic.entity.SubjectMapping;
import com.hzw.subject.infra.basic.service.SubjectLabelService;
import com.hzw.subject.infra.basic.service.SubjectMappingService;
import com.mysql.cj.x.protobuf.MysqlxCrud;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.LinkedList;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：xxx
 * @description：TODO
 * @date ：2025/2/19 16:15
 */

@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {
    @Resource
    private SubjectLabelService subjectLabelService;
    @Resource
    private SubjectMappingService subjectMappingService;
    @Override
    public Boolean add(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelController.add.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE
                .convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELEETED.getCode());
        return subjectLabelService.insert(subjectLabel);
    }

    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE
                .convertBoToLabel(subjectLabelBO);
        return subjectLabelService.update(subjectLabel);
    }

    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE
                .convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELEETED.getCode());
        return subjectLabelService.update(subjectLabel);
    }

    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        Integer categoryId = subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping=new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELEETED.getCode());
        List<SubjectMapping> mappingList=subjectMappingService.queryLabelByCategoryId(subjectMapping);
        if(CollectionUtils.isEmpty(mappingList)){
            return Collections.emptyList();
        }
        List<Integer> LabelIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        List<SubjectLabel>subjectLabels=subjectLabelService.batchQueryById(LabelIdList);
        List<SubjectLabelBO> bos=new LinkedList<>();
        subjectLabels.forEach(subjectLabel -> {
            SubjectLabelBO bo =new SubjectLabelBO();
            bo.setId(subjectLabel.getId());
            bo.setLabelName(subjectLabel.getLabelName());
            bo.setCategoryId(categoryId);
            bos.add(bo);
        });
//        List<SubjectLabelBO>                                                                                                                                                                    bos=SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabels);
        return bos;
    }
}
